package com.manage.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@EnableKnife4j
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private Boolean enable;

    @Bean
    public Docket createRestApiTwo() {
//        publicToken();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.manage.project.controller"))
                .paths(PathSelectors.any())
                .build().enable(enable).groupName("项目");
    }

//    @Bean
//    public Docket common() {
////        publicToken();
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.manage.project.controller.*"))
//                .paths(PathSelectors.any())
////                .build().enable(enable).globalOperationParameters(publicToken()).groupName("公共接口");
//                .build().groupName("公共接口");
//    }

//    public List<Parameter> publicToken() {
//        ParameterBuilder ticketPar = new ParameterBuilder();
//        List<Parameter> pars = new ArrayList<>();
//        ticketPar.name("Authorization").description("access_token")
//                .defaultValue("Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI4ODgiLCJleHAiOjE2NDEzMDMwMjV9.PkZWwqLy1d1JNJ9zyhmIIq06MrHFim8jNWl4rVZ1sxE")
//                .modelRef(new ModelRef("string")).parameterType("header")
//                .build(); //header中的ticket参数非必填，传空也可以
//        pars.add(ticketPar.build());    //根据每个方法名也知道当前方法在设置什么参数
//        return pars;
//    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口api")        // 文档页标题
                .contact(new Contact("资源管理",
                        "",
                        ""))        // 联系人信息
                .description("")  // 详细信息
                .version("1.0.0")   // 文档版本号
                .termsOfServiceUrl("") // 网站地址
                .build();
    }

}

